package com.example.demo.controller;

import com.example.demo.bean.Student;
import com.example.demo.pojo.Learn;
import com.example.demo.service.LearnService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;
import java.util.Random;

/**
 * Created by dingkl on 2017/7/12.
 */
@Controller
@RequestMapping("/learn")
public class LearnController {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    LearnService learnService;

    @RequestMapping("/learnShow")
    public String  learnShow(ModelMap map){
        logger.info("ces======");
        Student student = new Student();
        student.setAge(23);
        student.setName("dkl");
        map.put("student",student);
        //ModelAndView modelAndView = new ModelAndView("/learn");
        //modelAndView.addObject("student", student);
        return "learn";
    }

    @RequestMapping("/getLearn")
    public String getLearn(Long id,ModelMap modelMap){
        logger.info("ID:"+id);
        Learn learn = new Learn();
        learn.setId(id);
        Learn rlearn = learnService.getById(learn);
        modelMap.put("learn",rlearn);
        return "learnshow";
    }

    @RequestMapping("/getLearns")
    public String getLearns(int page,int pageSize,ModelMap modelMap){
        logger.info("page:"+page+",pageSize"+pageSize);
        List<Learn> learns = learnService.getList(page,pageSize);
        modelMap.put("learns",learns);
        modelMap.put("key","2");
        return "learnshows";
    }


    @RequestMapping("/addLearn")
    public String addLearn(int type){
        Learn learn = new Learn();
        learn.setId(new Random().nextLong());
        learn.setAuthor("ces1");
        learn.setTitle("ces2");
        learn.setUrl("http:www.baidu.com");
        if (type==1){
            learn.setId(2001l);
        } else if (type==2){
            learn.setId(2002l);
        }
        try {
            logger.info("flag:"+learnService.add(learn));
            logger.info(learn.getTitle());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "head";
    }
}
